// pages/writing/writing.js
const app = getApp()
var api = require('../../../utils/api.js')
var util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page:1,
    limit:20,
    content:"",
    dataList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.topicList();
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      page:1,
      content:''
    })
    this.topicList();
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },
  topicList: function () {
    var params = new Object();
    var that = this;
    var page = this.data.page;
    params.page= this.data.page;
    params.limit = this.data.limit;
    params.content = this.data.content;
    api.POST({
      params: params,
      url: app.globalData.base_url + 'topic/getList',
      success: function (res) {
        wx.hideLoading()
        if (util.isEmpty(res.data.code)) {
          var paging = res.data.data.paging;
          var totalPage = paging.total_page;
          var resultList = res.data.data.list;

          if (page == 1) {
            var itemData = resultList;
          } else {
            var itemData = that.data.dataList.concat(resultList)
          }
          that.setData({
            dataList: itemData
          })
        }
      },
      fail: function (fail) {
        wx.hideLoading()
        util.showError("网路错误，请检查网络");

      }
    })
  },
  //加载更多
  loadMore: function () {
    if (this.data.page >= this.data.total_page) {
      return;
    }
    wx.showLoading({
      title: '加载中',
    })
    this.setData({
      page: this.data.page + 1
    });
    this.topicList();
  },

  //刷新页面
  resetData: function () {
    var that = this
    that.setData({
      page: 1,
      hasMore: true
    });
  },
  //页面下拉刷新
  onPullDownRefresh: function () {
    var that = this;

    setTimeout(function () {
      that.resetData()
    }, 1000)
  },
  //页面上拉触底
  onReachBottom: function (e) {
    this.loadMore();
  },
  search:function(e){
    var content = e.detail.value
    this.setData({
      content: content,
      page:1
    })
    this.topicList();
  },
  writingDetail:function(e){
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/center/pages/talkInfo/talkInfo?id='+id
    })
  },
  published:function(){
    wx.navigateTo({
      url: '/center/pages/publishedTalk/publishedTalk'
    })
  }
})